package com.epoint.mobileoa.tools.controls.tree;

import java.util.HashMap;
import java.util.List;

import android.os.Message;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.bizlogic.Task;
import com.epoint.mobileoa.db.DBHelp;
import com.epoint.mobileoa.model.CodeItemsModel;

/**
 * @author liyc
 * @time 2012-4-16 下午3:10:19
 * @annotation 
 */

public class GetTreeDataTask extends Task {

	private String ItemName;
	private String style;
	private boolean unleafchoose;

	public GetTreeDataTask(SuperView con, HashMap<String, Object> param) {
		super(con, param);
	}

	@Override
	public void exectue(Message mess) throws Exception {

		String rootitemvalue = getParam().get("rootitemvalue").toString();
		ItemName = getParam().get("ItemName").toString();
		style = getParam().get("style")==null?null:getParam().get("style").toString();
		unleafchoose = (Boolean) getParam().get("unleafchoose");
		int type = (Integer) getParam().get("type");
		//创建根节点
		Node root = new Node("类别列表",null,null);
		root.setIcon(R.drawable.tree_ex);//设置图标
		
		setNode(rootitemvalue, root);
		
		root.setCheckBox(false);
		
		TreeAdapter ta = new TreeAdapter(getCon(),root,type);
		ta.setCheckBox(true);
		ta.setExpandedCollapsedIcon(R.drawable.tree_ex, R.drawable.tree_ec);
		ta.setExpandLevel(1);

		setMessageToRefreshUI(mess, getCon(), ta);

	}

	public void setNode(String parentitemvalue,Node node)
	{

		List<CodeItemsModel> list = getData(parentitemvalue);

		for(int i=0;i<list.size();i++){

			Node n = new Node(list.get(i).ItemText,list.get(i).ItemValue,list.get(i).CodeName);
			n.setParent(node);

			n.setIcon(R.drawable.icon_police);
			n.setCheckBox(true);

			if(n.getParent()!=null){
				n.getParent().setIcon(R.drawable.icon_department);
				n.getParent().setCheckBox(true);
			}

			if(unleafchoose){
				if(n.getChildren()!=null){
					n.getParent().setIcon(R.drawable.icon_department);
					n.getParent().setCheckBox(false);
				}
			}

			n.setExpanded(false);
			node.add(n);
			setNode(list.get(i).ItemValue,n);
		}
	}

	private List<CodeItemsModel> getData(String parentitemvalue) {
		List<CodeItemsModel> list = DBHelp.getChildCodeItems(getCon(), ItemName, parentitemvalue,style);
		return list;
	}

}
